package com.ngc.mall.controller;

import com.ngc.mall.common.configure.response.ResponseResult;
import com.ngc.mall.domain.CouponDomain;
import com.ngc.mall.domain.CouponTemplateDomain;
import com.ngc.mall.dto.CouponDTO;
import com.ngc.mall.dto.CouponFilterDTO;
import com.ngc.mall.service.ICouponService;
import com.ngc.mall.service.ICouponTemplateService;
import com.ngc.mall.service.IUserCouponService;
import com.ngc.mall.vo.PagingVO;
import com.ngc.mall.vo.UserCouponSimplifyVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * 优惠券
 * @author: chenxiaolin
 * @date 2023/8/21 15:05
**/
@RestController
@RequestMapping("/coupon")
public class CouponController {

    @Resource
    private ICouponTemplateService iCouponTemplateService;

    @Resource
    private ICouponService iCouponService;

    @Resource
    private IUserCouponService iUserCouponService;

    @PostMapping("/template")
    @ApiOperation("新增优惠券模板")
    public ResponseResult<Long> createCouponTemplate(@RequestBody CouponTemplateDomain domain){
        return ResponseResult.success(iCouponTemplateService.createCouponTemplate(domain));
    }

    @PutMapping("/template/id/{id}")
    @ApiOperation("根据id更新优惠券模板")
    public ResponseResult<Boolean> updateCouponTemplate(@PathVariable Long id,@RequestBody CouponTemplateDomain domain){
        return ResponseResult.auto(iCouponTemplateService.updateTemplateById(id,domain));
    }

    @GetMapping("/template/list")
    @ApiOperation("查看所有优惠券模板")
    public ResponseResult<List<CouponTemplateDomain>> findAllCouponTemplate(){
        return ResponseResult.success(iCouponTemplateService.findAll());
    }

    @DeleteMapping("/template/id/{id}")
    @ApiOperation("根据id删除优惠券模板")
    public ResponseResult<Boolean> deleteById(@PathVariable Long id){
        return ResponseResult.auto(iCouponTemplateService.deleteTemplateById(id));
    }

    @GetMapping("/user/collect")
    @ApiOperation("查看所有用户领取优惠券记录")
    public ResponseResult<List<UserCouponSimplifyVO>> findAllUserCoupon(){
        return ResponseResult.success(iUserCouponService.findAllCollectRecord());
    }

    @PostMapping("")
    @ApiOperation("新增(发布)优惠券")
    public ResponseResult<Long> createCoupon(@RequestBody CouponDTO dto){
        return ResponseResult.success(iCouponService.createCoupon(dto));
    }

    @GetMapping("/list")
    @ApiOperation("查看所有优惠券")
    public ResponseResult<List<CouponDomain>> findAllCoupon(){
        return ResponseResult.success(iCouponService.findAllCoupon());
    }

    @PostMapping("search")
    @ApiOperation("搜索优惠券")
    public ResponseResult<PagingVO<CouponDomain>> search(@RequestBody CouponFilterDTO dto, @RequestParam(name = "start", defaultValue = "0")
            Integer start,
                                                         @RequestParam(name = "count", defaultValue = "10")
                                                                     Integer count){
        return ResponseResult.success(iCouponService.search(dto,start,count));
    }

    @GetMapping("/list/enable")
    @ApiOperation("获取当前可用优惠券")
    public ResponseResult<List<CouponDomain>> findAllEnableCoupon(){
        return ResponseResult.success(iCouponService.findAllEnableCoupon());
    }

    @GetMapping("/list/by_days")
    @ApiOperation("日期类型为天数的优惠券")
    public ResponseResult<List<CouponDomain>> findDaysCoupon(){
        return ResponseResult.success(iCouponService.findDaysCoupon());
    }

}
